import operator
import copy

allLsomer = []
substituentsNum = 4

def zeroList():
    return [0,0,0,0,0,0]

class lsomer:
    def __init__(self, losmerList, addNum):
        self.losmerList = losmerList
        self.addNum = addNum
        self.setTypeNum()

    def setTypeNum(self):
        self.typeNum = 0
        for i in range(1, substituentsNum+1):
            for j in self.losmerList:
                if j == i:
                    self.typeNum += 1
                    break

    def next(self, lsomerList):
        firstElm = lsomerList[0]
        for i in range(1, len(lsomerList)):
            lsomerList[i-1] = lsomerList[i]
        lsomerList[len(lsomerList) - 1] = firstElm

    def isEqual(self, lsomer1List, lsomer2List):
        return operator.eq(lsomer1List, lsomer2List)

    def isMatch(self, lsomer2):
        for _ in range(6):
            self.next(self.losmerList)
            if self.isEqual(self.losmerList, lsomer2.losmerList):
                return True
            reverseLosmerList = copy.copy(self.losmerList)
            reverseLosmerList.reverse()
            if self.isEqual(reverseLosmerList, lsomer2.losmerList):
                return True
        return False

    def toStr(self):
        ret = ''
        for i in self.losmerList:
            ret += str(i)
        return ret

    def matchOrAdd(self):
        if self.typeNum > 3:
            return
        selfLsomer = copy.copy(self.losmerList)
        for lsomer2 in allLsomer:
            if self.addNum == lsomer2.addNum and self.isMatch(lsomer2):
                return
        self.losmerList = selfLsomer
        allLsomer.append(self)

maxAddNum = 6

if __name__ == '__main__':
    def recu(losmerList, addNum, chooseThis, nowSub=0):
        if addNum==maxAddNum or nowSub==6: # 退出条件
            obj = lsomer(losmerList, addNum)
            obj.matchOrAdd()
        else:
            if chooseThis:
                for i in range(1, substituentsNum+1):  # 可以放三种取代物
                    newLosmerList = copy.copy(losmerList)
                    newLosmerList[nowSub] = i
                    recu(newLosmerList, addNum + 1, True, nowSub + 1)
                    recu(newLosmerList, addNum + 1, False, nowSub + 1)
            else:
                recu(losmerList, addNum, True, nowSub + 1)
                recu(losmerList, addNum, False, nowSub + 1)

    # 递归起点
    recu(zeroList(), 0, True)
    recu(zeroList(), 0, False)

    cat = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[]}
    for lsomer2 in allLsomer:
        cat[lsomer2.addNum].append(lsomer2)
        losmerStr = lsomer2.toStr()
        if (not '33' in losmerStr) and (not (losmerStr[0]=='3' and losmerStr[len(losmerStr)-1]=='3')):
            for i in range(0, substituentsNum + 1):
                strI = str(i)
                if strI in losmerStr:
                    print(losmerStr+strI)
        else:
            # 中间必有房子
            print(losmerStr + '3')